package ua.org.nuos.sdms.clientgui.client.view;

import com.vaadin.data.Validator;
import com.vaadin.ui.*;
import ua.org.nuos.sdms.clientgui.client.components.PasswordForm;
import ua.org.nuos.sdms.clientgui.client.components.factory.ButtonFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.FormFactory;
import ua.org.nuos.sdms.clientgui.client.components.factory.LayoutFactory;
import ua.org.nuos.sdms.clientgui.client.components.fieldFactory.UserFieldFactory;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.RegisterNewUserEvent;
import ua.org.nuos.sdms.clientgui.client.model.EntityBeanItem;
import ua.org.nuos.sdms.clientgui.server.AppProxy;
import ua.org.nuos.sdms.middle.entity.User;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 01.04.12
 * Time: 17:42
 * To change this template use File | Settings | File Templates.
 */
public class RegisterNewUserComponent {

    protected VerticalLayout content;

    protected EntityBeanItem<User> userBeanItem;
    protected Form form;

    protected Button close, register;

    public RegisterNewUserComponent() {
        init();
    }

    protected void init() {
        content = new VerticalLayout();
        content.setSizeFull();
        content.setSpacing(true);

        userBeanItem = new EntityBeanItem<User>(new User());

        VerticalLayout formLayout = new VerticalLayout();
        formLayout.setSizeFull();
        formLayout.setSpacing(true);
        form = new PasswordForm();
        form.setLayout(formLayout);
        FormFactory.initForm(
                form, new UserFieldFactory(), userBeanItem, new String[]{"firstName", "lastName", "email", "password"});

        Layout buttons = getButtonsLayout();

        content.setSizeFull();
        content.setSpacing(true);
        content.addComponent(form);
        content.addComponent(buttons);
        content.setComponentAlignment(buttons, Alignment.BOTTOM_RIGHT);
    }

    protected Layout getButtonsLayout() {
        close = ButtonFactory.getButton("Не стоит", getCancelClickListener());
        register = ButtonFactory.getButton("Регистрация", getRegisterClickListener());
        ButtonFactory.setButtonColorStyle(register, ButtonFactory.Color.BLUE);
        return LayoutFactory.getHLayout(register, close);
    }

    protected Button.ClickListener getRegisterClickListener() {
        return new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                try {
                    form.commit();
                    RegisterNewUserEvent ev = new RegisterNewUserEvent(userBeanItem.getBean());
                    AppProxy.getInstance().getController(GlobalBoard.ID).fire(ev);
                } catch (Validator.InvalidValueException e) {
                    Logger.getAnonymousLogger().log(Level.WARNING, "validation exception", e);
                }
            }
        };
    }

    protected Button.ClickListener getCancelClickListener() {
        return new Button.ClickListener() {

            @Override
            public void buttonClick(Button.ClickEvent event) {
                userBeanItem.setBean(new User());
                form.discard();
            }
        };
    }

    public VerticalLayout getContent() {
        return content;
    }

}
